/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import com.ecarx.systemui.plugin.utils.LogUtils

enum class StatusBarItem {
    TIME,
    MOBILE_SIGNAL,
    WIFI,
    BLUETOOTH,
    USB,
    CENTER_LOCK,
    MESSAGE,
    LOCATION,
    AIR_PURGE,
    WPC,
    USER,
    GPS,
    APA,
    VOICE_CHAT,
}

class StatusBarView : ConstraintLayout {


    /**
     * just modify the order in this map to adapt to different project UX requirements
     */
    private var statusBarViewsOrderMap = linkedMapOf(

        StatusBarItem.TIME to TimeView(context),
        StatusBarItem.MOBILE_SIGNAL to MobileSignalView(context),
        StatusBarItem.WIFI to WifiView(context),
        StatusBarItem.BLUETOOTH to BluetoothView(context),
        StatusBarItem.USB to UsbView(context),
        StatusBarItem.CENTER_LOCK to CenterLockView(context),
        StatusBarItem.MESSAGE to MessageView(context),
        StatusBarItem.LOCATION to CarLocationView(context),
        StatusBarItem.AIR_PURGE to AirPurgeView(context),
        StatusBarItem.WPC to WpcView(context),
        StatusBarItem.USER to UserView(context),
        StatusBarItem.GPS to GpsView(context),
        StatusBarItem.APA to ApaView(context),
        StatusBarItem.VOICE_CHAT to VoiceChatView(context),

        )

    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {
        init()
    }


    fun init() {

        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        val timeView = TimeView(context).also { it.id = generateViewId() }
        val userView = UserView(context).also { it.id = generateViewId() }

        var prev = timeView.id
        for (item in statusBarViewsOrderMap) {

            when (item.key) {
                StatusBarItem.USER -> {
                    addView(userView, getUserIconLayoutParams())
                }
                StatusBarItem.TIME -> {
                    addView(timeView, getTimeIconLayoutParams())
                }
                else -> {
                    val params = getStatusIconLayoutParams()
                    addView(item.value, params.also {
                        it.endToStart = prev
                    })
                    LogUtils.d(TAG, item.key.name + "  id:" + item.value.id + "  prev:" + prev)
                    prev = item.value.id
                }
            }

        }

    }


    private fun getUserIconLayoutParams(): LayoutParams {
        return LayoutParams(80, 80).also {
            it.topToTop = LayoutParams.PARENT_ID
            it.bottomToBottom = LayoutParams.PARENT_ID
            it.startToStart = LayoutParams.PARENT_ID
        }
    }

    private fun getTimeIconLayoutParams(): LayoutParams {
        return LayoutParams(180, 60).also {
            it.topToTop = LayoutParams.PARENT_ID
            it.bottomToBottom = LayoutParams.PARENT_ID
            it.endToEnd = LayoutParams.PARENT_ID
        }
    }

    private fun getStatusIconLayoutParams(): LayoutParams {
        return LayoutParams(60, 60).also {
            it.topToTop = LayoutParams.PARENT_ID
            it.bottomToBottom = LayoutParams.PARENT_ID
            it.marginEnd = 24
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()

    }

    companion object {
        private const val TAG = "StatusBarView"
    }
}